package com.shanjupay.merchant.controller;

import com.shanjupay.common.domain.PageVO;
import com.shanjupay.common.util.QRCodeUtil;
import com.shanjupay.merchant.api.MerchantService;
import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.api.dto.StoreDTO;
import com.shanjupay.merchant.common.util.SecurityUtil;
import com.shanjupay.transaction.api.TransactionService;
import com.shanjupay.transaction.api.dto.QRCodeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/*
* 门店管理
*
*
* */
@Api(value = "商户平台-门店管理", tags = "商户平台-门店管理", description = "商户平台-门店的增删改查")
@Slf4j
@RestController
public class StoreController {

    /*
    * 查询门店列表
    *
    * */
    //1远程调用merchant 商户里面分页查询门店服务接口
    @Reference
    MerchantService merchantService;

    @Reference
    TransactionService transactionService;

    //从配置文件中进行读取商品标题 和 商品付款人
    @Value("${shanjupay.c2b.subject}")
    String subject;
    @Value("${shanjupay.c2b.body}")
    String body;

    //分页查询门店
    @ApiOperation("查询门店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页码",dataType = "int",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "当前页显示记录",required = true,dataType = "int",paramType = "query")
    })
    @PostMapping("/my/stores/merchants/page")
    public PageVO<StoreDTO> queryStoreByPage(Integer pageNo, Integer pageSize){
        //使用SecurityUtil工具类获取merchantId
        Long merchantId = SecurityUtil.getMerchantId();
        //设置必要参数
        //1创建StoreDTO
        StoreDTO storeDTO = new StoreDTO();
        storeDTO.setMerchantId(merchantId);
        //使用生成代理对象调用dubbo提供服务
        PageVO<StoreDTO> storeDTOS = merchantService.queryStoreByPage(storeDTO, pageNo, pageSize);
        return storeDTOS;
    }

    //生成商户二维码
    @ApiOperation("生成商户的二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="appId",value = "应用id",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name="storeId",value = "门店id",required = true,dataType = "String",paramType = "path")
    }
    )
    @GetMapping(value = "/my/apps/{appId}/stores/{storeId}/app-store-qrcode")
    public String  createCScanBStoreQRCode(@PathVariable("appId") String appId,@PathVariable("storeId") Long storeId) throws IOException {
        //qrCodeDto参数
        //从token中进行获取商户id
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchantDTO = merchantService.queryMerchantById(merchantId);
        //1创建qrCodeDto
        QRCodeDto qrCodeDto = new QRCodeDto();
        qrCodeDto.setAppId(appId);
        qrCodeDto.setStoreId(storeId);
        qrCodeDto.setSubject(String.format(subject,merchantDTO.getMerchantName()));
        qrCodeDto.setBody(String.format(body,merchantDTO.getMerchantName()));
        qrCodeDto.setMerchantId(merchantId);
        //2调用交易服务提供生成二维码的URL地址
        String qrCodeURL = transactionService.createStoreQRCode(qrCodeDto);

        //打印日志输出appid merchantid storeID
        log.info("[merchantId:{},appId:{},storeId:{}]createCScanBStoreQRCode is {}",merchantId,appId,storeId,qrCodeURL);
        //调用生成二维码的工具类
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        String qrCode = qrCodeUtil.createQRCode(qrCodeURL, 200, 200);
        return qrCode;
    }

}
